Lab Assignment 4

Due date : September 8, 12 PM EST

Total Marks : 40

 

 

Topics

Lab Exercises

Points

Variables

Constants

  • Area and Circumference of a Circle        
  • Painting a Room
  • 10+10

 

  • 10+10

 

 

Important Note:

·         Work with your partner collaboratively.

·         Work on one computer only.

·         Only one partner should upload all assignment files. The other partner will upload only the declaration form.

·         Make sure that these two programs compile and run without any error. A program with compiler or runtime  error will receive no credit.

 

Area and Circumference of a Circle

 

Study the program below, which uses both variables and constants:

 

//**********************************************************

//  Circle.java

//

//  Print the area of a circle with two different radii

//**********************************************************

 

public class Circle

{

    public static void main(String[] args)

    {

     final double PI = 3.14159;

    

     int radius = 10;

     double area = PI * radius * radius;

 

     System.out.println("The area of a circle with radius " + radius +

                        " is " + area);

 

     radius = 20;

     area = PI * radius * radius;

 

     System.out.println("The area of a circle with radius " + radius +

                        " is " + area);

 

    }

}

 

 

1.   When the radius of a circle doubles, what happens to its circumference and area? Do they double as well? You can determine this by dividing the second area by the first area. Unfortunately, as it is now the program overwrites the first area with the second area (same for the circumference). You need to save the first area and circumference you compute instead of overwriting them with the second set of computations. So you'll need two area variables and two circumference variables, which means they'll have to have different names (e.g., area1 and area2). Remember that each variable will have to be declared. Modify the program as follows:

·      Change the names of the area and circumference variables so that they are different in the first and second calculations. Be sure that you print out whatever you just computed.

·      At the end of the program, compute the area change by dividing the second area by the first area. This gives you the factor by which the area grew. Store this value in an appropriately named variable (which you will have to declare).

·      Add a println statement to print the change in area that you just computed.

·      Now repeat the last two steps for the circumference.

Look at the results. Is this what you expected?

 

2.   In the program above, you showed what happened to the circumference and area of a circle when the radius went from 10 to 20. Does the same thing happen whenever the radius doubles, or were those answers just for those particular values? To figure this out, you can write a program that reads in values for the radius from the user instead of having it written into the program ("hardcoded"). Modify your program as follows:

·      At the very top of the file, add the line

 

import java.util.Scanner;

 

This tells the compiler that you will be using methods from the Scanner class.  In the main method create a Scanner object called scan to read from System.in.

·      Instead of initializing the radius in the declaration, just declare it without giving it a value. Now add two statements to read in the radius from the user:

·      A prompt, that is, a print statement that tells the user what they are supposed to do (e.g., "Please enter a value for the radius.");

·      A read statement that actually reads in the value. Since we are assuming that the radius is an integer, this will use the nextInt() method of the Scanner class.

·      When the radius gets it second value, make it be twice the original value.

·      Compile and run your program.

 

Painting a Room

 

File Paint.java contains the partial program below, which when complete will calculate the amount of paint needed to paint the walls of a room of the given length and width. It assumes that the paint covers 350 square feet per gallon.

 

//***************************************************************

//File: Paint.java

//

//Purpose: Determine how much paint is needed to paint the walls

//of a room given its length, width, and height

//***************************************************************

import java.util.Scanner;

 

public class Paint

{

    public static void main(String[] args)

    {

        final int COVERAGE = 350;  //paint covers 350 sq ft/gal

        //declare integers length, width, and height;

        //declare double totalSqFt;

        //declare double paintNeeded;

       //declare and initialize Scanner object

 

        //Prompt for and read in the length of the room

 

        //Prompt for and read in the width of the room

 

        //Prompt for and read in the height of the room

 

        //Compute the total square feet to be painted--think

        //about the dimensions of each wall

 

        //Compute the amount of paint needed

 

        //Print the length, width, and height of the room and the

        //number of gallons of paint needed.

    }

}

 

Save this file to your directory and do the following:

 

1.   Fill in the missing statements (the comments tell you where to fill in) so that the program does what it is supposed to. Compile and run the program and correct any errors.

2.   Suppose the room has doors and windows that don't need painting. Ask the user to enter the number of doors and number of windows in the room, and adjust the total square feet to be painted accordingly. Assume that each door is 20 square feet and each window is 15 square feet.

 

Submit: Submit the following files.

·         Circle.java

·         Paint.java